package eu.codlab.androidemu.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class ZipRead {
	private boolean _gbc;
	private boolean _gba;

	public ZipInputStream openZip(File file) throws FileNotFoundException{
		return new ZipInputStream(new FileInputStream(file));
	}
	public void compute(File file){
		_gba = false;
		_gbc = false;
			Log.d("Pokemon",file.getAbsolutePath());
		ZipInputStream zip;
		try {
			zip = openZip(file);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			zip = null;
		}

		if(zip == null)
			return;

		ZipEntry entry = null;
		int number = 0;
		do{
			try {
				entry = zip.getNextEntry();
				if(entry != null && entry.isDirectory())
					entry = null;
			} catch (IOException e) {
				entry = null;
				e.printStackTrace();
			}
			if(entry != null){
				Log.d("Pokemon",entry.getName());
				if(entry.getName().endsWith(".gba")){
					_gba = true;
				}else if(entry.getName().endsWith(".gbc") ||
						entry.getName().endsWith(".gb")){
					_gbc = true;
				}
			}
			number++;
		}while(entry != null && number<3);//number < 20 cause normal zip file will never host more than 1 gb/gbc/gba!
		try {
			zip.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public boolean isGBA(){
		return _gba;
	}

	public boolean isGBC(){
		return _gbc;
	}
}
